#include "miniutil/strutil.h"
#include "miniutil/xmlutil.h"

namespace miniutil { namespace xmlutil {

   //file:elea.eleb:attribute
    int GetValue(const std::string& key, std::string &value)
    {
        std::vector<std::string> tagattrs = miniutil::strutil::split(key, ":");
        if(tagattrs.size() < 2)
        {
            return -1;
        }
        
        std::string xmlfn = tagattrs[0]+".xml";
        miniutil::tinyxml::TiXmlDocument tidoc;
        bool bret = tidoc.LoadFile(xmlfn);
        if(!bret)
        {
            perror(NULL);
            return -1;
        }
        
        std::string fikey = tagattrs[1];
        for(unsigned int i=2; i< tagattrs.size(); i++)
        {
            fikey +=  std::string(":")+tagattrs[i];
        }
        
        int ret = GetValue(&tidoc, fikey, value);
        return ret;
    }
    
   //file:elea.eleb:attribute
   int UpdateValue(const std::string& key, const std::string& value)
    {
        std::vector<std::string> tagattrs = miniutil::strutil::split(key, ":");
        if(tagattrs.size() < 2)
        {
            return -1;
        }
        
        std::string xmlfn = tagattrs[0]+".xml";
        miniutil::tinyxml::TiXmlDocument tidoc;
        bool bret = tidoc.LoadFile(xmlfn);
        if(!bret)
            return -1;
        
        std::string fikey = tagattrs[1];
        for(unsigned int i=2; i< tagattrs.size(); i++)
        {
            fikey +=  std::string(":")+tagattrs[i];
        }
        
        int ret = UpdateValue(&tidoc, fikey, value);
        if(ret == 0)
            tidoc.SaveFile();
        return ret;
    }
        
   //elea.eleb:attribute
   int GetValue(miniutil::tinyxml::TiXmlNode* proot, const std::string& key,  std::string &value)
    {
        std::vector<std::string> tagattrs = miniutil::strutil::split(key, ":");
        
        std::vector<std::string> tags = miniutil::strutil::split(tagattrs[0], ".");
        if(tags.empty()/*tags.size() < 1*/)
        {
            return -1;
        }
        
        miniutil::tinyxml::TiXmlElement* pele = proot->FirstChildElement(tags[0]);
        if(pele == NULL)
            return -1;

        for(unsigned int i=1; i< tags.size(); i++)
        {
            pele = pele->FirstChildElement(tags[i]);
            if(pele == NULL)
                return -1;
        }

        if(tagattrs.size() == 1)
        {
            value << (*pele);
            return 0;
        }
        else if(tagattrs.size() == 2)
        {
            const char* pattr = pele->Attribute(tagattrs[1].c_str());
            if(pattr == NULL)
                return -1;
            value = pattr;
            return 0;
        }
        return -1;
    }
    
   //elea.eleb:attribute
   int UpdateValue(miniutil::tinyxml::TiXmlNode* proot, const std::string & key, const std::string& value)
    {
        std::vector<std::string> tagattrs = miniutil::strutil::split(key, ":");
        
        std::vector<std::string> tags = miniutil::strutil::split(tagattrs[0], ".");
        if(tags.empty()/*tags.size() < 1*/)
        {
            return -1;
        }

        miniutil::tinyxml::TiXmlElement* pele = proot->FirstChildElement(tags[0]);
        if(pele == NULL)
            return -1;

        for(unsigned int i=1; i< tags.size(); i++)
        {
            pele = pele->FirstChildElement(tags[i]);
            if(pele == NULL)
                return -1;
        }
         
        if(tagattrs.size() == 2)
        {
            if(value.empty())
                pele->RemoveAttribute(tagattrs[1]);
            else
                pele->SetAttribute(tagattrs[1], value);
            return 0;                
        }
        else if(tagattrs.size() == 1)
        {
            if(value.empty())
                pele->Clear();
            else
            {
                miniutil::tinyxml::TiXmlDocument newdoc;
                newdoc.Parse(value.c_str());
                pele->Clear();
                if(newdoc.RootElement() != NULL)
                {
                    miniutil::tinyxml::TiXmlElement* pchi = newdoc.RootElement();
                    while(pchi != NULL)
                    {
                        pele->InsertEndChild(*pchi);
                        pchi = pchi->NextSiblingElement();
                    }
                }
                else
                {
                    pele->InsertEndChild(miniutil::tinyxml::TiXmlText(value));
                }                
            }
            return 0;                                
        }
        
        return -1;            
    }
        
} }

    